﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Task;
using Backend.JYD.Common;

namespace Backend.JYD.EFDAL.DAL
{
    public class TaskBaseInfoDAL : Backend.JYD.IDAL.Task.ITaskBaseInfo
	{
		#region 查询数据TaskBaseInfo

        public PageInfo<TaskBaseInfo> GetTaskBaseInfoByEmpInfo(PageInfo page, List<Int64> roleIds, Int64 empId, DateTime? start, DateTime? end, string status, string code)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskBaseInfo> query = db.TaskBaseInfoes;
                if (roleIds != null && roleIds.Count > 0)
                {
                    if (empId > 0)
                    {
                        var ids = roleIds.Select(p => p.ToString());
                        string id = "," + empId.ToString() + ",";
                        query = from p in db.TaskBaseInfoes
                                from p1 in ids
                                where ("," + p.RoleIds + ",").Contains("," + p1 + ",") || ("," + p.EmpIds + ",").Contains(id)
                                select p;
                    }
                    else
                    {
                        var ids = roleIds.Select(p => p.ToString());
                        query = from p in db.TaskBaseInfoes
                                from p1 in ids
                                where ("," + p.RoleIds + ",").Contains("," + p1 + ",")
                                select p;
                    }
                    query = query.Distinct();
                }
                else if (empId > 0)
                {
                    string id = "," + empId.ToString() + ",";
                    query = query.Where(p => ("," + p.EmpIds + ",").Contains(id));
                }
                if (!string.IsNullOrWhiteSpace(status))
                {
                    string[] statuses = status.Split(',');
                    List<Int32> ids = new List<int>();
                    foreach (string s in statuses) { if (!string.IsNullOrWhiteSpace(s)) { ids.Add(NumberHelper.StrToInt(s, -1)); } }
                    Int32[] id = ids.ToArray();
                    query = query.Where(p => id.Contains(p.Status));
                }
                if (start.HasValue)
                {
                    query.Where(p=>(!p.UpdateTime.HasValue? p.CreateTime.Value : p.UpdateTime.Value) >= start.Value);
                } 
                if (end.HasValue)
                {
                    query.Where(p => (!p.UpdateTime.HasValue ? p.CreateTime.Value : p.UpdateTime.Value) < end.Value);
                }
                if (!string.IsNullOrWhiteSpace(code))
                {
                    int step = NumberHelper.StrToInt(code);
                    query = from p in query
                                 where p.StepId  == step
                                 select p;
                }
                query = query.OrderByDescending(p => p.Status).ThenByDescending(p => p.CreateTime);
                //其他条件代码
                return new PageInfo<TaskBaseInfo>(page, query);
            }
        }

        public PageInfo<TaskBaseInfo> GetOldTaskBaseInfoByEmpInfo(PageInfo page, List<Int64> roleIds, Int64 empId, DateTime? start, DateTime? end, string status, string code)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes;
                if (roleIds != null && roleIds.Count > 0)
                {
                    if (empId > 0)
                    {
                        var ids = roleIds.Select(p => p.ToString());
                        string id = "," + empId.ToString() + ",";
                        query = from p in db.TaskStepInfoes
                                from p1 in ids
                                where ("," + p.RoleIds + ",").Contains("," + p1 + ",") || ("," + p.EmpIds + ",").Contains(id)
                                select p;
                    }
                    else
                    {
                        var ids = roleIds.Select(p => p.ToString());
                        query = from p in db.TaskStepInfoes
                                from p1 in ids
                                where ("," + p.RoleIds + ",").Contains("," + p1 + ",") 
                                select p;
                    }
                }
                else if (empId > 0)
                {
                    string id = "," + empId.ToString() + ",";
                    query = query.Where(p => ("," + p.EmpIds + ",").Contains(id));
                }
                if (!string.IsNullOrWhiteSpace(status))
                {
                    string[] statuses = status.Split(',');
                    List<Int32> ids = new List<int>();
                    foreach (string s in statuses) { if (!string.IsNullOrWhiteSpace(s)) { ids.Add(NumberHelper.StrToInt(s, -1)); } }
                    Int32[] id = ids.ToArray();
                    query = query.Where(p => id.Contains(p.Status));
                }
                if (start.HasValue)
                {
                    query.Where(p=>(!p.UpdateTime.HasValue? p.CreateTime.Value : p.UpdateTime.Value) >= start.Value);
                } 
                if (end.HasValue)
                {
                    query.Where(p => (!p.UpdateTime.HasValue ? p.CreateTime.Value : p.UpdateTime.Value) < end.Value);
                }
                if (!string.IsNullOrWhiteSpace(code))
                {
                    code = code.ToLower();
                    var query1 = from p in query
                                 from p1 in db.TaskStepModels
                                 where p1.TaskModeId == p.TaskBaseInfo.TaskModelId && p1.StepId == p.StepId && p1.StepCode.ToLower() == code
                                 select p;
                    query = query1;
                }
                query = query.OrderByDescending(p => p.Status).ThenByDescending(p => p.CreateTime);
                query = query.Distinct();
                var query2 = from p in query
                             select p.TaskBaseInfo;
                //其他条件代码
                return new PageInfo<TaskBaseInfo>(page, query2);
            }
        }
        
		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<TaskBaseInfo> GetTaskBaseInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<TaskBaseInfo> query = db.TaskBaseInfoes;
				//其他条件代码
				return new PageInfo<TaskBaseInfo>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public TaskBaseInfo GetTaskBaseInfoByTaskId(Int64 taskId)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<TaskBaseInfo> query = db.TaskBaseInfoes.Where(p => p.TaskId == taskId);
				return query.FirstOrDefault();
			}
		}

        /// <summary>
        /// 查询数据
        /// </summary>
        public IList<TaskBaseInfo> GetTaskBaseInfoByTaskIds(IList<Int64> taskIds)
        {
            Int64[] ids = taskIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskBaseInfo> query = db.TaskBaseInfoes.Where(p => ids.Contains(p.TaskId));
                return query.ToList();
            }
        }

		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据TaskBaseInfo
		/// </summary>
		public Int64 AddTaskBaseInfo(TaskBaseInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.TaskBaseInfoes.Add(info);
				db.SaveChanges();
				return info.TaskId;
			}
		}
		#endregion

		#region 修改数据TaskBaseInfo

		/// <summary>
		/// 根据字段（TaskId）修改数据TaskBaseInfo
		/// </summary>
		public bool UpdateTaskBaseInfo(TaskBaseInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				var query = db.TaskBaseInfoes.Where(p => p.TaskId == info.TaskId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
                        temp.EmpIds = info.EmpIds;
                        temp.RoleIds = info.RoleIds;
                        temp.Status = info.Status;
                        temp.StepId = info.StepId;
                        temp.TaskDetailId = info.TaskDetailId;
                        temp.Type = info.Type;
                        temp.StepId = info.StepId;

						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion



        public long AddTaskBaseInfo_Old(TaskBaseInfo_Old info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                db.TaskBaseInfo_Old.Add(info);
                db.SaveChanges();
                return info.TaskId;
            }
        }
    }
}